<?php

namespace App\Http\Controllers\Company;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DataController extends Controller
{
    /**
     * 核销课程数据
     */
    public function course(Request $request){
        $companyId = $request->company_id;
        $beginDate = $request->begin_date;
        $endDate = $request->end_date;
        $courses = $request->courses;
        $teachers = $request->teachers;

        if(!$beginDate) $beginDate = date('Y-m-01');

        $query = DB::table('student_sign as ss')
        ->leftJoin('student as st', 'ss.student_id', '=', 'st.id')
        ->leftJoin('course as c', 'ss.course_id', '=', 'c.id')
        ->leftJoin('course_arrange as ca', 'ss.arrange_id', '=', 'ca.id')
        ->leftJoin('teacher as t', 'ca.teacher_id', '=', 't.id')
        ->leftJoin('class as cl', 'ca.class_id', '=', 'cl.id')
        ->select('st.name as student_name', 'c.name as course_name', 't.name as teacher_name', 'cl.name as class_name',
        'ss.student_id', 'ss.course_id', 't.id as teacher_id',
        'ss.date', 'ss.result', 'ss.course_num', 'ss.course_money', 'ss.created_at', 'ss.arrange_id')
        ->where('ss.company_id', $companyId)
        ->where('ss.date', '>=', $beginDate);
        if($endDate) $query->where('ss.date', '<=', $endDate);
        if($courses) $query->whereIn('ss.course_id', $courses);
        if($teachers) $query->whereIn('t.id', $teachers);
        
        $data = $query->orderBy('ss.id', 'desc')->get();
        return self::ok($data);
    }
}
